package com.itheima.util;

import java.util.ArrayList;
import java.util.Random;
import java.util.TreeSet;

/*
生成双色球
 */
public class Ball {

    private static Random r = new Random();
    private static int getRed() {
        return r.nextInt(33) + 1;
    }
    private static int getBlue() {
        return r.nextInt(16) + 1;
    }

    /**
     * 判断指定的number再集合中是否存在
     * @param set  红球的集合
     * @param number 需要被判断的值
     * @return  true 存在  false   不存在
     */
    private static boolean getIndex(TreeSet<Integer> set, int number) {
        if (set == null || set.size() < 0) {
            return false;
        }
        for (Integer i : set) {
            if(i==number){
                return true;
            }
        }
        return false;
    }

    public static ArrayList<Integer> getBall(){
        TreeSet<Integer> redBall = new TreeSet<>();
        while(redBall.size()<=6){
            int red = getRed();
            boolean isExist =redBall.contains(red) ;
            if(isExist){
               //存在
            }else{
                //不存在
                boolean before1 = getIndex(redBall, red - 1);
                boolean before2 = getIndex(redBall, red - 2);
                boolean before3 = getIndex(redBall, red - 3);
                boolean after1 = getIndex(redBall, red + 1);
                boolean after2 = getIndex(redBall, red + 2);
                boolean after3 = getIndex(redBall, red + 3);
                if (before1  && before2 && before3 ) {
                    continue;
                }
                if (before1  && before2  && after1 ) {
                    continue;
                }
                if (before1  && after1  && after2 ) {
                    continue;
                }
                if (after1  && after2  && after3 ) {
                    continue;
                }
                redBall.add(red);
            }

        }
        ArrayList<Integer> result = new ArrayList<>(redBall);
      /*  for (Integer ball : redBall) {
            result.add(ball);
        }*/
        result.add(getBlue());
        return result;
    }

}
